<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *售后列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'用户考勤'),
	array('op'=>'list2','title'=>'考勤总表'),
	array('op'=>'class','title'=>'导出考勤表'),
	array('op'=>'log','title'=>'考勤记录'),
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['classid']){
		$params['classid']=intval($_RQ['classid']);
		$return['params']['classid']=$_RQ['classid'];
	}elseif($this->session('classid')){
		$params['classid']=$this->session('classid');
		$return['params']['classid']=$this->session('classid');
	}
	if(!empty($_RQ['title'])){
		$params2=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
		$xueshengids=$this->getall('xuesheng',$params2,array('id','title'),'id');
		if($xueshengids){
			$xueshengids=array_keys($xueshengids);
		}else{
			$xueshengids=-1;
		}
		$params['xueshengid']=$xueshengids;
	}
	if($_RQ['kao_day']){
		$return['params']['kao_day']=$_RQ['kao_day'];
		$this->model('uplog')->upclasskaoqing_log($this->session('classid'),$_RQ['kao_day']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_xuesheng',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$day=$_RQ['kao_day']?:date('Y-m-d',time());
		$class_kc=pdo_get('jy_yxw_class_kc',array('day'=>$day,'classid'=>$this->session('classid'),'uniacid'=>$_SESSION['uniacid']));
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['id']);
			$item['goodnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>0,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['cdnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>1,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['bjnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>2,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['sjnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>3,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['kknum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>4,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['type']=pdo_getcolumn('jy_yxw_kaoqing_log',array('day'=>$day,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'type');
			$item['kejiantitle']=$this->getcolumn('kejian',array('id'=>$class_kc['kejianid'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_message'=>'确认删除该售后吗？','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'sn','title'=>'售后编号','type'=>'string'),
			array('name'=>'kechengid','title'=>'使用','type'=>'select2','options'=>'kechengs'),
			array('name'=>'kejiantitle','title'=>'课件','type'=>'string'),
			array('name'=>'type','title'=>$day.'-考勤','type'=>'radio2','options'=>'type'),
			array('name'=>'classid','title'=>'售后群','type'=>'select2','options'=>'class'),
		),'action'=>array(
			array('type'=>'href','action'=>'jy_yxw/xuesheng.kaoqing/log/','title'=>'查看/校正考勤'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/xuesheng.kaoqing/settype/type=0','title'=>'一键正常打卡'),
			array('type'=>'post','url'=>'/xuesheng.kaoqing/settype/type=1','title'=>'一键迟到打卡'),
			array('type'=>'post','url'=>'/xuesheng.kaoqing/settype/type=2','title'=>'一键病假打卡'),
			array('type'=>'post','url'=>'/xuesheng.kaoqing/settype/type=3','title'=>'一键事假打卡'),
			array('type'=>'post','url'=>'/xuesheng.kaoqing/settype/type=4','title'=>'一键旷课打卡'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后名称','name'=>'title','help'=>''),
			//array('name'=>'classid','width'=>'15','title'=>'售后群','type'=>'select','options'=>'class'),
			array('name'=>'kao_day','width'=>10,'title'=>'日期','type'=>'time','time_type'=>'day'),
			//array('name'=>'storeid','width'=>'15','title'=>'所属门店','type'=>'select','options'=>'stores'),
		),
	);
	//$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['class']=$this->getall('class',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['kechengs']=$this->getall('kecheng',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['type']=array('正常','迟到','病假','事假','旷课');
	$return['isday']=array('未考勤','已考勤');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='list2'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['classid']){
		$params['classid']=intval($_RQ['classid']);
		$return['params']['classid']=$_RQ['classid'];
	}elseif($this->session('classid')){
		$params['classid']=$this->session('classid');
		$return['params']['classid']=$this->session('classid');
	}
	if(!empty($_RQ['title'])){
		$params2=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
		$xueshengids=$this->getall('xuesheng',$params2,array('id','title'),'id');
		if($xueshengids){
			$xueshengids=array_keys($xueshengids);
		}else{
			$xueshengids=-1;
		}
		$params['xueshengid']=$xueshengids;
	}
	if($_RQ['kao_day']){
		$return['params']['kao_day']=$_RQ['kao_day'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_xuesheng',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$day=$_RQ['kao_day']?:date('Y-m-d',time());
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['id']);
			$item['goodnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>0,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['cdnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>1,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['bjnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>2,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['sjnum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>3,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['kknum']=pdo_count('jy_yxw_kaoqing_log',array('type'=>4,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['type']=pdo_getcolumn('jy_yxw_kaoqing_log',array('day'=>$day,'xueshengid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'type');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'sn','title'=>'售后编号','type'=>'string'),
			array('name'=>'classid','title'=>'售后群','type'=>'select','options'=>'class'),
			array('type'=>'string','title'=>'正常天数','name'=>'goodnum'),
			array('type'=>'string','title'=>'迟到次数','name'=>'cdnum'),
			array('type'=>'string','title'=>'病假次数','name'=>'bjnum'),
			array('type'=>'string','title'=>'事假次数','name'=>'sjnum'),
			array('type'=>'string','title'=>'旷课次数','name'=>'kknum'),
		),'action'=>array(
			array('type'=>'href','action'=>'jy_yxw/xuesheng.kaoqing/log/','title'=>'查看记录'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后名称','name'=>'title','help'=>''),
			array('name'=>'kao_day','width'=>10,'title'=>'日期','type'=>'time','time_type'=>'day'),
		),
	);
	//$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['class']=$this->getall('class',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['type']=array('正常','迟到','病假','事假','旷课');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='settype'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择售后','error');
	}
	$type=intval($_RQ['type']);
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$this->model('uplog')->addkaoqing_log($id,$type);
		}
	}
	$types=array('正常','迟到','病假','事假','旷课');
	exi($types[$type].'打卡成功！');
}
if($_RQ['op']=='log'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($this->session('classid')){
		$params['classid']=$this->session('classid');
		$return['params']['classid']=$this->session('classid');
	}
	$return['params']['status']=$_RQ['status'];
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['id']){
		$params['xueshengid']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_kaoqing_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['xueshengid']);
			$item['kejiantitle']=$this->getcolumn('kejian',array('id'=>$item['kejianid'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、这里显示考勤资料！
		','del_message'=>'确认删除该考勤吗？','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'kechengid','title'=>'使用','type'=>'select2','options'=>'kechengs'),
			array('name'=>'kejiantitle','title'=>'课件','type'=>'string'),
			array('name'=>'type','title'=>'考勤类型','type'=>'radio2','options'=>'kaoqing_type'),
			array('type'=>'string','title'=>'备注','name'=>'remark'),
			array('name'=>'day','title'=>'日期','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','action'=>'/xuesheng.kaoqing/remark/','title'=>'备注'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后姓名','name'=>'title','help'=>''),
			array('name'=>'type','width'=>'15','title'=>'考勤类型','type'=>'radio','options'=>'kaoqing_type'),
		),
	);
	$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['kechengs']=$this->getall('kecheng',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['kaoqing_type']=$this->kaoqing_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='class'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($this->session('classid')){
		$params['id']=$this->session('classid');
		$return['params']['classid']=$this->session('classid');
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
    $orderby='id desc';
	$total='';
	$return['list']=$this->getall('class',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>这里显示本门店的售后群；
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'售后群名称','type'=>'string'),
			array('name'=>'workerid','title'=>'售后主管','type'=>'select2','options'=>'workers'),
			array('name'=>'kechengid','title'=>'绑定使用','type'=>'select2','options'=>'kechengs'),
			array('name'=>'keshi','title'=>'课时','type'=>'string'),
			array('name'=>'xueshengnum','title'=>'用户人数','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio2','options'=>'status'),
			array('name'=>'open_time','title'=>'开班时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','action'=>'/xuesheng.kaoqing/export/','title'=>'选择月份导出'),
			array('type'=>'export','action'=>'/xuesheng.kaoqing/export1/','title'=>'导出上月考勤'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后群名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	//$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['workers']=$this->getall('worker',array('role'=>11,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['kechengs']=$this->getall('kecheng',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['status']=$this->class_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='addremark'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$this->model('uplog')->addkaoqing_log($id,$post['type'],$post['day'],$post['remark']);
		exi('操作成功！','','/xuesheng.kaoqing/log/');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'添加考勤',
	);
	$htmls['edit']=array(
		array('name'=>'day','title'=>'考勤日期','type'=>'time','time_type'=>'day'),
		array('type'=>'radio','title'=>'考勤类型','name'=>'type','options'=>'type'),
		array('type'=>'text','title'=>'备注','name'=>'remark'),
	);
	$return['type']=array('正常','迟到','病假','事假','旷课');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='remark'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		pdo_update('jy_yxw_kaoqing_log',array('remark'=>$post['remark'],'type'=>$post['type']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','/xuesheng.kaoqing/log/');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_kaoqing_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'备注',
	);
	$htmls['edit']=array(
		array('type'=>'radio','title'=>'考勤类型','name'=>'type','options'=>'type'),
		array('type'=>'text','title'=>'备注','name'=>'remark'),
	);
	$return['type']=array('正常','迟到','病假','事假','旷课');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后群','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'按月导出考勤',
	);
	$htmls['edit']=array(
		array('name'=>'title','title'=>'售后群','type'=>'copy'),
		array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
		array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
	);
	$year=intval(date('Y',time()));
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	if(!$post['year']){
		exi('请选择年份！','error');
	}
	if(!$post['month']){
		exi('请选择月份！','error');
	}
	$lasttime=strtotime($post['year'].'-'.$post['month'].'-01');
	$url=download_url('jy_yxw/xuesheng.kaoqing/export1/id='.$post['id'].'&lasttime='.$lasttime);
	exi('点确定开始下载','','',$url);
}
if($_RQ['op']=='export1'){
	$return['params']=array();
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['classid']=intval($_RQ['id']);
	}else{
		exi('请选择售后群！','error');
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$lasttime=$_RQ['lasttime']?:strtotime('-1 month');
	$year =date('Y',$lasttime);
	$month =date('m',$lasttime);
	$day = date("t",strtotime($year.'-'.$month));
	$header=array(
		'DAY'=>'编号',
		'日期'=>'姓名',
	);
	$i = 1;
	$weektype=array('日','一','二','三','四','五','六');
	while ($i <= $day){
		$date=$year . '-' . $month . '-' . $i;
		$week=date('w', strtotime($date));
		$header[$i]=$weektype[$week];
		++$i;
	}
	$header['出勤']='天数';
	$list=pdo_getall('jy_yxw_xuesheng',$params,$fields);
	$newslist=array();
	if($list){
		foreach($list as $k=>$nv){
			$nitem=array();
			$nitem['DAY']=$k+1;
			$nitem['日期']=$nv['title'];
			$i = 1;
			$times=0;
			while ($i <= $day){
				$date=$year . '-' . $month . '-' . $i;
				$$week=date('w', strtotime($date));
				$log=pdo_get('jy_yxw_kaoqing_log',array('day'=>$date,'xueshengid'=>$nv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','type'));
				if($log['id']){
					if(!$log['type']){
						$nitem[$i]='√';
						$times ++;
					}elseif($log['type']==1){
						$nitem[$i]='-';
					}elseif($log['type']==2){
						$nitem[$i]='+';
					}elseif($log['type']==3){
						$nitem[$i]='±';
					}elseif($log['type']==4){
						$nitem[$i]='○';
					}
				}
				++$i;
			}
			$nitem['出勤']=$times;
			$newslist[]=$nitem;
		}
	}
	$classtitle=pdo_getcolumn('jy_yxw_class',array('id'=>$params['classid'],'uniacid'=>$_SESSION['uniacid']),'title');
	model('core/tools')->export2excel($header,$newslist,$classtitle.'['.$year . '-' . $month . ']考勤',true);
}